package com.example.exam.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author zhang
 */

@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //保存数据
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    //带过期时间的保存数据
    public void set(String key, String value, long time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.MINUTES);
    }

    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    //获取数据
    public Object get(String key, Class<?> clazz) {
        String jsonString = redisTemplate.opsForValue().get(key);
        return JsonUtils.s2o(jsonString, clazz);
    }

    //删除数据
    public void delete(String key) {
        redisTemplate.delete(key);
    }

}
